package com.example.lotterytest.controller;

import com.example.lotterytest.commen.MybatisUtils;
import com.example.lotterytest.entity.Award;
import com.example.lotterytest.entity.Winner;
import com.example.lotterytest.mapper.LotteryMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/6 20:03
 */
// 抽奖管理类
public class LotteryManager{

    private final int lotteryNumber;
    private final List<Award> awards;
    private final List<Winner> winners;
    private final Random random;
    private SqlSession sqlSession;

    public LotteryManager(int lotteryNumber, List<Award> awards) {
        this.lotteryNumber = lotteryNumber;
        this.awards = awards;
        this.winners = new ArrayList<>();
        this.random = new Random();
        this.sqlSession =  MybatisUtils.getSqlSession();
    }

    // 从指定奖项中抽取一个获奖人员
    public synchronized Winner drawWinner(Award award) {
        if (award.getCount() > 0) {
            int id = winners.size() + 1; // 生成新的获奖者ID
            int index = random.nextInt(1000); // 模拟抽奖，可根据需求修改

            //获奖概率为6%
            if (index < lotteryNumber) {
                Winner winner = new Winner(id, "Winner_" + id, award.getId());
                winners.add(winner);
                award.decrementCount(); // 减少奖项数量
                LotteryMapper lotteryMapper = sqlSession.getMapper(LotteryMapper.class);
                lotteryMapper.save(winner);
                return winner;
            }
        }
        return null;
    }

    // 执行抽奖过程
    public void runLottery() {
        //使用线程池模拟多人抽奖
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        //模拟1000个人同时抽奖
        for(int i = 0;i<1000;i++){
            for (Award award : awards) {
                executorService.execute(() -> {
                    while (true) {
                        //某个奖可以让谁抽中
                        Winner winner = drawWinner(award);
                        if (winner != null) {
                            System.out.println("Congratulations! " + winner.getName() +
                                    " won " + award.getDescribe());


                        } else {
                            System.out.println("很遗憾，您没有中奖");
                            break; // 奖项已抽完，结束循环
                        }
                    }
                });
            }
        }


        executorService.shutdown();
    }

    public static void main(String[] args) {
        // 参考配置
        int lotteryNumber = 60;
        List<Award> awards = List.of(
                new Award(1, "一等奖", 1),
                new Award(2, "二等奖", 2),
                new Award(3, "三等奖", 3)
        );

        // 初始化抽奖管理器并运行抽奖
        LotteryManager lotteryManager = new LotteryManager(lotteryNumber, awards);
        lotteryManager.runLottery();
    }

}
